package com.doit.day04

import scala.io.Source

/**
 *
 * order.txt
*order011,u001,300
*order012,u002,200
*order023,u006,100
*order056,u007,300
*order066,u003,500
*order055,u004,300
*order021,u005,300
*order014,u001,100
*order025,u005,300
*order046,u007,30
*order067,u003,340
*order098,u008,310
 *
 *user.txt
*u001,hls,22,fengjie
*u002,wangwu,31,lisi
*u003,zhangyanru,22,tananpengyou
*u004,laocao,26,fengyi
*u005,mengqi,12,nvmengqi
*u006,haolei,38,sb
*u007,wanghongjing,24,wife
*u009,wanghongjing,24,wife
 *
 *
 *返回一个结果：order011  u001   300  hls  22   fengjie
 */
object _join_案例 {
  def main(args: Array[String]): Unit = {

    val order: List[String] = Source.fromFile("data/order.txt").getLines().toList
    val user: List[String] = Source.fromFile("data/user.txt").getLines().toList

    //将一行数据转变成元祖对象
    val order_map: List[(String, String, String)] = order.map(line => {
      val arr: Array[String] = line.split(",")
      //order_id,user_id,price
      (arr(0), arr(1), arr(2))
    })

    val user_map: Map[String, (String, String, String)] = user.map(line => {
      val arr: Array[String] = line.split(",")
      //user_id name age nickname
      (arr(0), (arr(1), arr(2), arr(3)))
    }).toMap


    for (elem <- order_map) {
      val user_id: String = elem._2
      val maybeTuple: Option[(String, String, String)] = user_map.get(user_id)
      if(maybeTuple.isDefined){
        val value: (String, String, String) = maybeTuple.get
        println((elem._1,elem._2,elem._3,value._1,value._2,value._3))
      }else{
//        println((elem._1,elem._2,elem._3,null,null,null))
      }

    }










    //这种就是join
    //可以是可以，但是效率不高
/*    for (elem1 <- order_map) {
      val user_id: String = elem1._2
      for (elem2 <- user_map) {
        if(user_id.equals(elem2._1)){
          println((elem1._1,elem1._2,elem1._3,elem2._2,elem2._3,elem2._4))
        }
      }
    }*/









  }

}
